package com.yumis.service.modules.user.controller;

import com.yumis.service.common.enums.HttpState;
import com.yumis.service.common.vo.Response;
import com.yumis.service.model.pojo.User;
import com.yumis.service.modules.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 添加用户信息
     * @param user
     * @return
     */
    @PostMapping("addUser")
    public ResponseEntity<Response> addUser(@Valid User user){
        userService.addUser(user);
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK));
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PostMapping("updateUser")
    public ResponseEntity<Response> updateUser(@Valid User user){
        userService.updateUser(user);
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK));
    }

    /**
     * 根据id删除用户信息
     * @param id
     * @return
     */
    @PostMapping("deleteUserById")
    public ResponseEntity<Response> deleteUserById(@RequestParam("id") Long id){
        userService.deleteUserById(id);
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK));
    }

    /**
     * 根据id批量删除用户信息
     * @param ids
     * @return
     */
    @PostMapping("deleteUser")
    public ResponseEntity<Response> deleteUser(@RequestParam("id") Long[] ids){
        userService.deleteUser(ids);
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK));
    }

    /**
     * 查询所有的用户
     * @return
     */
    @GetMapping("queryUser")
    public ResponseEntity<Response> queryUser(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                              @RequestParam(value = "rows", defaultValue = "10") Integer rows){
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK,userService.queryUser(page,rows)));
    }

    /**
     * 根据用户姓名查询用户信息(模糊搜索)车主电话/车牌号码/车主姓名
     * @return
     */
    @GetMapping("searchUser")
    public ResponseEntity<Response> searchUser(@RequestParam(value = "key", required = false) String key){
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK,userService.searchUser(key)));
    }

    /**
     * 根据店铺id查询车主信息
     * @param shopId
     * @return
     */
    @PostMapping("searchUserByShop")
    public ResponseEntity<Response> searchUserByShop(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                                     @RequestParam("shopId") Long shopId){
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK,userService.searchUserByShop(page,rows,shopId)));
    }

    /**
     * 根据id查询车主信息
     * @param userId
     * @return
     */
    @PostMapping("searchUserById")
    public ResponseEntity<Response> searchUserById(@RequestParam("userId") Long userId){
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK,userService.searchUserById(userId)));
    }
}
